/**
 * 图片压缩转base64
 * @param {file} base64 base64图片数据
 * @param {number} quality 图片质量
 * @param {string} format 输出图片格式
 * @return {base64} data 图片处理完成后的base64
 */
import EXIF from 'exif-js'
const MAX_WIDTH = 800
export const compress = (base64, quality, mimeType, orientation) => {
  let img = document.createElement('img')
  img.crossOrigin = 'anonymous'
  return new Promise((resolve, reject) => {
    img.src = base64
    // 图片偏移值
    let offetX = 0
    img.onload = () => {
      let imgW = img.width,
        imgH = img.height,
        resizeW = 0,
        resizeH = 0
      if (imgW > MAX_WIDTH) {
        let scale = Math.max(imgW / MAX_WIDTH, imgH / MAX_WIDTH)
        resizeW = imgW / scale
        resizeH = imgH / scale
      } else {
        resizeW = imgW
        resizeH = imgH
      }
      let cvs = document.createElement('canvas')
      let ctx = cvs.getContext("2d")
      console.log(orientation)
      if (orientation == 3) {
        cvs.width = resizeW;
        cvs.height = resizeH;
        ctx.rotate(Math.PI);
        ctx.drawImage(img, 0, 0, -resizeW, -resizeH)
      } else if (orientation == 8) {
        cvs.width = resizeH;
        cvs.height = resizeW;
        ctx.rotate(Math.PI * 3 / 2);
        ctx.drawImage(img, 0, 0, -resizeW, resizeH)
      } else if (orientation == 6) {
        cvs.width = resizeH;
        cvs.height = resizeW;
        ctx.rotate(Math.PI / 2);
        ctx.drawImage(img, 0, 0, resizeW, -resizeH)
      } else {
        cvs.width = resizeW;
        cvs.height = resizeH;
        ctx.drawImage(img, 0, 0, resizeW, resizeH)
      }
      // console.log(resizeW, resizeH, cvs.width, cvs.height)
      // ctx.drawImage(img, 0, 0, resizeW, -resizeH)
      let imageData = cvs.toDataURL(mimeType, quality / 100)
      resolve(imageData)
    }
  })
}
/**
 * 文件流转base64
 * @param {file} source 图片对象
 * @param {number} quality 图片质量
 * @return {promise} promise 图片处理promise
 */
export const fileToBase64ByQuality = (file, quality) => {
  let fileReader = new FileReader()
  let type = file.type
  let orientation
  return new Promise((resolve, reject) => {
    EXIF
      .getData(file, function () {
        orientation = EXIF.getTag(this, 'Orientation');
        // console.log(orientation)
        if (window.URL || window.webkitURL) {
          resolve(compress(URL.createObjectURL(file), quality, type, orientation))
        } else {
          fileReader.onload = () => {
            resolve(compress(fileReader.result, quality, type, orientation))
          }
          fileReader.onerror = (e) => {
            reject(e)
          }
          fileReader.readAsDataURL(file)
        }
      })
    // resolve(compress(file), quality, type)
  })
}
/**
 * base64转文件流
 * @param {base64} base64数据
 * @param {string} format格式
 * @return {file}  文件blob
 */
export const convertBase64UrlToBlob = (base64, mimeType) => {
  // debugger
  let bytes = window.atob(base64.split(',')[1])
  let ab = new ArrayBuffer(bytes.length)
  let ia = new Uint8Array(ab)
  for (let i = 0; i < bytes.length; i++) {
    ia[i] = bytes.charCodeAt(i)
  }
  let _blob = new Blob([ab], {type: mimeType})
  return _blob
}